"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : show create database
Case Name   : 创建schema进行查询
Create At   : 2022/09/20
@zou_jialiang0501120110
Description :
    1、create database
    2、create schema
    3、查询schema
    4、查询schema+if not exists
    5、清理环境
Expect      :
    1、创建成功
    2、创建成功
    3、查询成功
    4、查询成功
    5、清理环境成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class VariablesCase(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.commonsh = CommonSH("PrimaryDbUser")
        self.common = Common()
        self.constant = Constant()
        self.db_name = "db_var_0020"
        self.schema_name = "sch_variable_0020"

    def test_variables(self):
        step1_text = "-----step1:create database;expect:创建成功-----"
        self.logger.info(step1_text)
        sql_cmd1 = f'''drop database if exists {self.db_name};
                    create database {self.db_name} dbcompatibility = 'B';'''
        self.logger.info(sql_cmd1)
        sql_res1 = self.commonsh.execut_db_sql(sql_cmd1)
        self.logger.info(sql_res1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res1,
                      "创建数据库失败:" + step1_text)

        step2_text = "-----step2:create schema;expect:创建成功-----"
        self.logger.info(step2_text)
        sql_cmd2 = f"drop schema if exists {self.schema_name};" \
                   f"create schema {self.schema_name};"
        self.logger.info(sql_cmd2)
        sql_res2 = self.commonsh.execut_db_sql(sql_cmd2,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res2)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG, sql_res2,
                      "查询失败：" + step2_text)

        step3_text = "-----step3:查询schema;expect:查询成功-----"
        self.logger.info(step3_text)
        sql_cmd3 = f"show create database {self.schema_name};" \
                   f"show create schema {self.schema_name};"
        self.logger.info(sql_cmd3)
        sql_res3 = self.commonsh.execut_db_sql(sql_cmd3,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res3)
        self.assertTrue(sql_res3.count(
            f"CREATE SCHEMA {self.schema_name} AUTHORIZATION") == 2,
                        "查询失败：" + step3_text)

        step4_text = "-----step4:查询schema+if not exists;expect:查询成功-----"
        self.logger.info(step4_text)
        sql_cmd4 = f"show create database if not exists {self.schema_name};" \
                   f"show create schema if not exists {self.schema_name};"
        self.logger.info(sql_cmd4)
        sql_res4 = self.commonsh.execut_db_sql(sql_cmd4,
                                               dbname=f'{self.db_name}')
        self.logger.info(sql_res4)
        self.assertTrue(
            sql_res4.count(
                f"CREATE SCHEMA IF NOT EXISTS "
                f"{self.schema_name} AUTHORIZATION") == 2,
            "查询失败：" + step4_text)

    def tearDown(self):
        step5_text = "-----step5:清理环境;expect:成功-----"
        self.logger.info(step5_text)

        drop_db_text = "-----drop database-----"
        self.logger.info(drop_db_text)
        drop_db_cmd = f'''drop database if exists {self.db_name};'''
        self.logger.info(drop_db_cmd)
        drop_db_res = self.commonsh.execut_db_sql(drop_db_cmd)
        self.logger.info(drop_db_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      drop_db_res, "drop database filed" + drop_db_text)

        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
